﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

using WeixinSB.Tools;

namespace MSPrize.View
{
    /// <summary>
    /// 可用于自身或导航至 Frame 内部的空白页。
    /// </summary>
    public sealed partial class CommentDialogPage : Page
    {
        private readonly DialogService _service = new DialogService { AnimationType = DialogService.AnimationTypes.Fast };

        public event EventHandler<InputEventArgs> Closed;

        public string ReplyId { get; set; }

        public CommentDialogPage()
        {
            this.InitializeComponent();
            ContentGrid.Opacity = 0;
            _service.Child = this;
            _service.Opened += (sender, args) => ShowStoryboard.Begin();
#if WINDOWS_PHONE_APP
             Windows.Phone.UI.Input.HardwareButtons.BackPressed += HardwareButtons_BackPressed;
#endif
            ShowStoryboard.Completed += (sender, o) =>
            {

            };
            HideStoryboard.Completed += (sender, o) =>
            {
                _service.Hide();
#if WINDOWS_PHONE_APP
             Windows.Phone.UI.Input.HardwareButtons.BackPressed -= HardwareButtons_BackPressed;
#endif
            };
        }
#if WINDOWS_PHONE_APP
        void HardwareButtons_BackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e)
        {
            e.Handled = true;
            Close();
            if (Closed != null)
                Closed(this, new InputEventArgs());
        }
#endif
        public void Show()
        {
            _service.Show();
        }

        private void Close()
        {
            HideStoryboard.Begin();
        }
        public static readonly DependencyProperty TitleProperty =
               DependencyProperty.Register("Title", typeof(string), typeof(CommentDialogPage), new PropertyMetadata(""));
        public string Title
        {
            get { return (string)GetValue(TitleProperty); }
            set { SetValue(TitleProperty, value); }
        }

        public static readonly DependencyProperty PlaceholderTextProperty =
               DependencyProperty.Register("PlaceholderText", typeof(string), typeof(CommentDialogPage), new PropertyMetadata(""));
        public string PlaceholderText
        {
            get { return (string)GetValue(PlaceholderTextProperty); }
            set { SetValue(PlaceholderTextProperty, value); }
        }

        public static readonly DependencyProperty TextProperty =
           DependencyProperty.Register("Text", typeof(string), typeof(CommentDialogPage), new PropertyMetadata(""));
        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Close();
            if (Closed != null)
                Closed(this, new InputEventArgs());
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            Close();
            Text = inputText.Text;
            if (Closed != null)
                Closed(this, new InputEventArgs(Text, ReplyId));
        }
    }
    public class InputEventArgs : EventArgs
    {
        public string ReplyId { get; set; }
        public string Text { set; get; }

        public InputEventArgs() { }

        public InputEventArgs(string text, string replyId)
        {
            Text = text;
            ReplyId = replyId;
        }
    }
}
